package cts;

import java.util.Vector;

import javax.swing.JScrollPane;

import sensor.Sensor;
import houseHold.House;
import houseHold.Room;

public class GUIBuilder {
	private static Vector<Tab> currentTabList=new Vector<Tab>();
	private static Vector<TabContainer> tabContainer=new Vector<TabContainer>();
	
	public static TabContainer execute()
	{
		tabContainer=new Vector<TabContainer>();
		for(int i=0;i<House.getInstance().getRoomCount();i++)
		{
			currentTabList=new Vector<Tab>();
			Room r=House.getInstance().getRoom(i);
			r.builSensorLists();
			if(r.hasGeneral())
			{
				buildGeneralTab(r);
			}
			if(r.getElectricSensors()!=null)
			{
				buildElectricTab(r.getElectricSensors(),r);
			}
			if(r.getGasSensors()!=null)
			{
				buildGasTab(r.getGasSensors(),r);
			}
			if(r.getWaterSensors()!=null)
			{
				buildWaterTab(r.getWaterSensors(),r);
			}
			
			if(currentTabList.size()!=0)
			{
				TabContainer tc=new TabContainer();
				tc.setTabName(r.getName());
				for(int j=0;j<currentTabList.size();j++)
				{
					tc.addComponent(currentTabList.get(j).getName(), currentTabList.get(j).getTab());
				}
				tabContainer.add(tc);
			}
		}
		if(tabContainer.size()!=0){
			TabContainer masterTab=new TabContainer();
			for(int i=0;i<tabContainer.size();i++)
			{
				masterTab.addComponent(tabContainer.get(i).getTabName(), tabContainer.get(i));
			}
			return masterTab;
		}
		return null;
			
	}

	private static void buildGeneralTab(Room r) {
		Tab general=new Tab();
		general.setName("General");
		SensorPanel panel=new SensorPanel();
		if(r.getTempSensors()!=null)
			panel.addSensors(r.getTempSensors());
		if(r.getLightSensors()!=null)
			panel.addSensors(r.getLightSensors());
		if(r.getPresenceSensors()!=null)
			panel.addSensors(r.getPresenceSensors());
		if(r.getWindowSensors()!=null)
			panel.addSensors(r.getWindowSensors());
		panel.build(r);
		general.setTab(new JScrollPane(panel));
		currentTabList.add(general);
		
	}

	private static void buildGasTab(Vector<Sensor> gasSensors,Room r) {
		Tab gas=new Tab();
		gas.setName("Gas");
		SensorPanel panel=new SensorPanel();
		panel.addSensors(gasSensors);
		panel.build(r);
		gas.setTab(new JScrollPane(panel));
		currentTabList.add(gas);
		
	}


	private static void buildWaterTab(Vector<Sensor> waterSensors,Room r) {
		Tab water=new Tab();
		water.setName("Water");
		SensorPanel panel=new SensorPanel();
		panel.addSensors(waterSensors);
		panel.build(r);
		water.setTab(new JScrollPane(panel));
		currentTabList.add(water);
		
	}

	private static void buildElectricTab(Vector<Sensor> electricSensors,Room r) {
		Tab electric=new Tab();
		electric.setName("Electric");
		SensorPanel panel=new SensorPanel();
		panel.addSensors(electricSensors);
		panel.build(r);
		electric.setTab(new JScrollPane(panel));
		currentTabList.add(electric);
		
	}

}
